//
//  RegisterViewController.m
//  OneDay
//
//  Created by lanou3g on 15/11/9.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"用户注册";
    self.navigationController.navigationBar.translucent = NO;
    self.view.bounds = CGRectMake(0, 0, kWidth, kHeight - 64);
    
    // Do any additional setup after loading the view from its nib.
}

// 注册按钮
- (IBAction)registerUser:(UIButton *)sender {
    // 如果用户名,密码,邮箱,电话 为空
    if ([self.name.text isEqualToString:@""] || [self.password.text isEqualToString:@""] || [self.address.text isEqualToString:@""]){
        [self showAlterViewWithTitle:@"提示" Message:@"用户名,密码,邮箱不能为空"];
        
    } else {
        // 如果不为空的话
        [self registerWithUserName:self.name.text UserPassWord:self.password.text UserAddress:self.address.text];
    }

}

// 注册
- (void)registerWithUserName:(NSString *)name UserPassWord:(NSString *)passWord UserAddress:(NSString *)address
{
    AVUser *user = [AVUser user];
    user.username = name;
    user.password = passWord;
    user.email = address;
    
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            //  NSError *err = error;
            
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            switch (codeNum) {
                case 203:
                    [self showAlterViewWithTitle:@"提示" Message:@"该邮箱已经被占用"];
                    break;
                case 125:
                    [self showAlterViewWithTitle:@"提示" Message:@"该邮箱格式不正确"];
                    break;
                case 202:
                    [self showAlterViewWithTitle:@"提示" Message:@"用户名已经被占用"];
                    break;
                default:
                    break;
            }
            
            
        } else {
            [self showAlterViewWithTitle:@"提示" Message:@"注册成功"];
        }
    }];
}

// 封装一个alertView
- (void)showAlterViewWithTitle:(NSString *)title Message:(NSString *)message
{
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [self performSelector:@selector(closeAlter:) withObject:alertView afterDelay:1];
    [self presentViewController:alertView animated:YES completion:nil];
}
// 设置1秒之后消失
- (void)closeAlter:(UIAlertController *)alert
{
    [alert dismissViewControllerAnimated:YES completion:nil];
    
}

// 点击页面空白出 收取键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
